import { Card } from "./cardts"
export class Poker {
    cards:Array<Card> = new Array(24)
    suits:string[] = ['红桃', '黑桃', '方片', '草花']
    constructor() {
        let index = 0
        for (let i = 0; i < this.suits.length; i++) {
            for (let j = 9; j < 15; j++) {
                let card:Card = new Card(this.suits[i], j)
                this.cards[index++] = card
            }
        }
    }
    output_card():void {
        let index = 0
        for (let i = 0; i < this.suits.length; i++) {
            for (let j = 9; j < 15; j++) {
                if (index % 6 === 0 && index !== 0) {
                    console.log()
                }
                process.stdout.write(this.cards[index++] + ' ')
            }
        }
    }
    shuffle_card():void {
        for (let i = 0; i < this.cards.length; i++) {
            let random_index:number = Math.floor(Math.random() * this.cards.length)
            let tmp_card:Card = this.cards[i]
            this.cards[i] = this.cards[random_index]
            this.cards[random_index] = tmp_card
        }
    }
    get_one_hand():Card[] {
        this.shuffle_card()
        let onehand:Array<Card> = []
        for (let i = 0; i < 5; i++) {
            onehand.push(this.cards[i])
        }
        return onehand
    }

    output_onehand(onehand:Array<Card>):void{
        for (let card of onehand) {
            process.stdout.write(card + ' ')
        }
    }

    // get_special_one_hand() {
    //     let onehand = []
    //     let card = new card('红桃', 5)
    //     onehand.push(card)
    //     card = new card('黑桃', 5)
    //     onehand.push(card)
    //     card = new card('黑桃', 6)
    //     onehand.push(card)
    //     card = new card('黑桃', 7)
    //     onehand.push(card)
    //     card = new card('黑桃', 8)
    //     onehand.push(card)
    //     return onehand
    // }

get_one_card(index:number):Card{
return this.cards[index]
}

    get_type(onehand:Array<Card>):string{
        let suit_set:Set<string> = new Set()
        let rank_set:Set<number> = new Set()
        let card_map:Map<number,number> = new Map()
        for (let card of onehand) {
            suit_set.add(card.suit)
            rank_set.add(card.rank)
            if(card_map.has(card.rank)){
                let count:number|undefined = card_map.get(card.rank)
                if(count !== undefined){
                    count++
                    
                card_map.set(card.rank, count)
            }
        }
            else{
                card_map.set(card.rank,1)
            }
        }
       
       
       
        onehand.sort((a:Card, b:Card) => a.rank - b.rank)

        let isStraight = false
        let isSameColor = false
        if (suit_set.size === 1) {
            isSameColor = true
            //return'同花'
        }
        if (onehand[4].rank - onehand[0].rank === 4 && rank_set.size === 5) {
            isStraight = true
            // return'顺子'
        }
        if (isSameColor === true && isStraight === true) {
            return '同花顺'
        }
        if (isSameColor === true) {
            return '同花'
        }
        if (isStraight === true) {
            return '顺子'
        }
        if (rank_set.size === 4) {
            return '一对'
        }
        if (rank_set.size === 5) {
            return '无对'
        }
        if (rank_set.size === 2) {
            let arr=[...card_map.values()]
            if(arr.includes(4)){
                return '四条'
            }
            if(arr.includes(3)){
                return '满堂红'
            }
        }
        if (rank_set.size === 3) {
            let arr =[...card_map.values()]
            if(arr.includes(3)){
                    return '三条'
            }
            if(arr.includes(2)){
                return '两对'
            }
        
        }
        return 'unknown type'
    }
    
}
let poker:Poker = new Poker()
poker.output_card()
console.log();
console.log('洗牌之后');
console.log();
// poker.shuffle_card()

let hand:Array<Card> = poker.get_one_hand()
console.log();
poker.output_card()
console.log();
console.log('发出一手牌');
console.log();
poker.output_onehand(hand)
console.log();
let type:string = poker.get_type(hand)
console.log(type);
